package com.yueya.api.senior;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.business.senior.model.SeniorEgress;
import com.yueya.common.constant.CodeConst;
import com.yueya.common.model.Resp;
import com.yueya.common.service.BaseService;
import com.yueya.common.util.UUIDGenerator;

import net.sf.json.JSONObject;

public class ISeniorEgressService extends BaseService {

	/**
	 * 获取列表
	 * 
	 * @param jo
	 * @return
	 */
	public Resp getPageList(JSONObject jo) {
		if (containsKey(jo, "pgn", "pgs")) {

			if (jo.containsKey("pgn")) {
				pageNumber = jo.getInt("pgn");
			}
			if (jo.containsKey("pgs")) {
				pageSize = jo.getInt("pgs");
			}
			Page<Record> page = SeniorEgress.dao.paginate(pageNumber, pageSize, jo);
			if (page != null) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setData(page);
			} else {
				resp.setCode(CodeConst.WARN);
				resp.setMsg("no data");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("参数错误");
		}
		return resp;
	}

	// 获取明细
	public Resp getDetail(JSONObject jo) {
		if (containsKey(jo, "egressid")) {
			String id = jo.getString("egressid");
			StringBuffer sql = new StringBuffer("select se.egress_id egressid,se.egress_date egressdate,se.egress_reason egressreason,se.egress_note egressnote,");
			sql.append("vsb.senior_id seniorid,");
			sql.append("vsb.senior_name seniorname,");
			sql.append("vsb.senior_note seniornote,");
			sql.append("vsb.senior_type seniortype,");
			sql.append("vsb.self_type selftype,");
			sql.append("vsb.bedno,vsb.bedname,");
			sql.append("vsb.avatar_url avatarurl,");
			sql.append("vsb.id_card idcard");
			sql.append(" from jg_senior_egress se");
			sql.append(" left join v_senior_base vsb on vsb.senior_id = se.senior_id");
			sql.append(" where se.del_flag='0' and  se.egress_id =?  ");
			Record r = Db.findFirst(sql.toString(), id);
			if (r != null) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setData(r);
			} else {
				resp.setCode(CodeConst.WARN);
				resp.setMsg("no data");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("参数错误");

		}
		return resp;
	}

	/**
	 * 保存
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp save(JSONObject jo) {
		if (containsKey(jo, "seniorid")) {
			String seniorid = jo.getString("seniorid");
			String egressdate = jo.getString("egressdate");
			String egressreason = jo.getString("egressreason");
			String egressnote = jo.getString("egressnote");
			String registrantdate = jo.getString("registrantdate");

			String userid = jo.getString("userid");
			String egressid = UUIDGenerator.getUUID();
			SeniorEgress se = Duang.duang(SeniorEgress.class);
			se.set("egress_id", egressid);
			se.set("senior_id", seniorid);
			se.set("egress_date", egressdate);
			se.set("egress_reason", egressreason);
			se.set("egress_note", egressnote);
			se.set("registrant_user_id", userid);
			se.set("registrant_date", registrantdate);
			boolean bl = SeniorEgress.dao.save(se);
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("保存失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}

	/**
	 * 保存
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp update(JSONObject jo) {
		if (containsKey(jo, "egressid")) {
			String seniorid = jo.getString("seniorid");
			String egressdate = jo.getString("egressdate");
			String egressreason = jo.getString("egressreason");
			String egressnote = jo.getString("egressnote");
			String registrantdate = jo.getString("registrantdate");
			String egressid = jo.getString("egressid");
			String userid = jo.getString("userid");
			SeniorEgress se = Duang.duang(SeniorEgress.class);
			se.set("egress_id", egressid);
			se.set("senior_id", seniorid);
			se.set("egress_date", egressdate);
			se.set("egress_reason", egressreason);
			se.set("egress_note", egressnote);
			se.set("registrant_user_id", userid);
			se.set("registrant_date", registrantdate);
			boolean bl = SeniorEgress.dao.update(se);
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("保存失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}

	/**
	 * 删除
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp delete(JSONObject jo) {
		if (containsKey(jo, "egressid")) {
			String egressid = jo.getString("egressid");
			SeniorEgress r = SeniorEgress.dao.getDetailById(egressid);
			r.set("del_flag", "1");
			boolean b1 = SeniorEgress.dao.update(r);
			if (b1) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("删除成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("删除失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}
}
